# coding=utf-8
from bs4 import BeautifulSoup
import time
import re
from  spider.models import  Company,Job
import requests

class URLResponse:
    headers = {
        'Host':"www.51wincai.com",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36",
        "Content-Type" : "application/x-www-form-urlencoded",
        "Upgrade-Insecure-Requests" : "1"
    }
    def __init__(self, url):
        self.url = url
        
    def getHtml(self, method = 'get', data = {}):
        if method == 'get':
            return requests.get(self.url, headers = self.headers, timeout = 10)



class Wincai51:
    soup = None
    url = ''
    
    def initSoap(self, url):
        """
        初始化
        """
        all_the_text = URLResponse(url).getHtml()
        self.soup = BeautifulSoup(all_the_text.content)
        self.url = url
        return self.soup
    
    def urls(self, url):
        """
        抓取公司url
        """
        all_the_text = URLResponse(url).getHtml()
        self.soup = BeautifulSoup(all_the_text.content)
        cUrlsCompiles = [
            re.compile("/job/companydetails_([0-9]+).htm"),
        ]
        try:
            objs = self.soup.find(id="js-newJobs").find_all(href = cUrlsCompiles)
        except:
            return 
            
        urls = []
        if objs:
            for o in objs:        
                company_name =  self._filter(o.text)
                print(company_name)
                companys = Company.objects.filter(url=o['href'])
                if not companys:
                    cInstance = Company.objects.create(**{
                        "url" : o['href'],
                        "name" : company_name,
                        "email" : '',
                        "phone" : '',
                        "web" : '',
                        "area" : '%s|%s' % ('全国','fcjob88'),
                    })
                else:
                    cInstance = companys[0]    
                if cInstance:
                    cUrlsCompiles = [
                        re.compile("/job/([0-9]+).htm"),
                    ]
                    jobs = o.parent.parent.parent.parent.parent.find_all(href = cUrlsCompiles)
                    for j in jobs:
                        if not j.text:
                            continue
                        print(j.text)
                        jobInfo = Job.objects.filter(url__contains=j['href'])
                        if not jobInfo:
                            jInstance = Job.objects.create(**{
                                "url" : j['href'],
                                'company_id' : cInstance.id
                            })
                            print("51wincai-job: %s" %jInstance.id)
                        else:
                            print("51wincai has existed:%s" % j['href'])
                    #重置公司对象
                    cInstance = None
            
        return urls
    
    def getJobsUrls(self):
        """
        抓取职位url
        """
        cUrlsCompiles = [
                re.compile("/jobs/([a-zA-Z0-9]+).html"),
        ]
        objs = self.soup.find_all(href = cUrlsCompiles)
        urls = []
        for o in objs:
            urls.append({
                'job_url' : self._filter(o['href']),
                'job_name' : self._filter(o.text)
            })
            
        return urls
            
    def getCompanyUrls(self, name = None):
        """
        抓取公司url
        """
        cUrlsCompiles = [
            re.compile("/vvip/([a-zA-Z0-9]+)/"),
            re.compile("/Company_Detail.php?CompanyDetail=([a-zA-Z0-9]+)/"),
        ]
        objs = self.soup.find('ul',class_="post_list").find_all(href = cUrlsCompiles)
        urls = []
        for o in objs:              
            urls.append({
                'company_url' : self._filter(o['href']),
                'company_name' : self._filter(o.text)
            })
            
        return urls

    def getCompanyInfo(self, company=None):
        """
        公司信息
        """
        all_the_text = URLResponse(company.url).getHtml()
        self.soup = BeautifulSoup(all_the_text.content)
        try:
            objs = self.soup.find('div',class_="cc_left")
            comtypes = objs.find('ul', class_='cc_list clearfix mb30').find_all('li')
            for typ in comtypes:
                if '企业性质：' in typ.text:
                    company.typs = self._filter(typ.text.strip('企业性质：'))
                elif '企业规模：' in typ.text:
                    company.size = self._filter(typ.text.strip('企业规模：'))
                    
            company.profile = objs.find(id="companyDes").text
                    
            #company.url =  self.url       
            company.save()
        except:
            company.typs = 'failed'
            company.save()    
        return urls


    def getJobInfo(self, job = None):
        print(job.url)
        all_the_text = URLResponse(job.url).getHtml()
        self.soup = BeautifulSoup(all_the_text.content)
        try:
            
            jobObj = self.soup.find('div', class_="cc_left")
            job.name = self._filter(jobObj.find('h2', class_='cc_name').text)
            job.dated = self._filter(jobObj.find('p', class_='jd_post_time').text)
            jtags = jobObj.find('ul', class_='cc_list clearfix').find_all('li')
            for tag in jtags:
                if '学历：' in tag.text:
                    job.school = self._filter(tag.text.strip('学历：'))
                elif '招聘人数：' in tag.text:
                    job.number = self._filter(tag.text.strip('招聘人数：'))
                elif '经验：' in tag.text:
                    job.exper = self._filter(tag.text.strip('经验：'))
                elif '更新时间：' in tag.text:
                    job.dated = self._filter(tag.text.strip('更新时间：'))
                elif '地点：' in tag.text:
                    job.address = self._filter(tag.text.strip('地点：'))    
                    
            job.desc = self._filter(jobObj.find('div',class_='jd_detail mt30').text)
            job.money = self._filter(jobObj.find('p',class_='jd_salary').text)
            job.runed = 1
            job.save()
        except:
            job.runed = 1
            job.save()
        


    def _filter(self, strs):
        if strs:
            return strs.strip()
        return ""
    
    

